package com.hnevc.examliqiang;
import android.content.Context;
import android.content.Intent;
import android.os.Looper;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;
import com.squareup.picasso.Picasso;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.util.List;

import okhttp3.Call;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

public class MainActivity extends AppCompatActivity {
    private static final String TAG = "MainActivity";
    //TODO 定义变量
    Button mBtnLogin;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //TODO 题目1：获取控件
        mBtnLogin = (Button) findViewById(R.id.id_btn_login);
        //TODO 设置点击事件监听者
        mBtnLogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //TODO 获取用户的数据
                    //获取控件
                EditText etUsername = (EditText) findViewById(R.id.id_et_username);
                EditText etPassword = (EditText) findViewById(R.id.id_et_password);
                    //获取值
                final String username = etUsername.getText().toString();
                final String password = etPassword.getText().toString();

                //TODO 访问网络，提交登录信息
                    //创建线程
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        //访问网络
                        //准备数据_将变量构造成对json字符串，以便传输。
                        JSONObject object = new JSONObject();
                        try {
                            object.put("userName", username);
                            object.put("userPassword", password);
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                        String s = object.toString();

                        //准备URL
                        String url = "http://10.1.66.232:8080/RestaurantServer/user/doLogin";
                        //构建RequestBody
                        RequestBody body = RequestBody.create(MediaType.parse("application/json"), s);
                        //创建Request对象
                        Request request = new Request.Builder()
                                .post(body)
                                .url(url)
                                .build();
                        //构建Call，并执行
                        OkHttpClient client = new OkHttpClient();
                        Call call = client.newCall(request);
                        try {
                            //执行网络访问获取结果数据，保存到response对象中
                            Response response = call.execute();
                            //将response对象的文字部分取出来，存放到data中
                            String data = response.body().string();

                            Log.e(TAG, "run: " + data);

                        //处理结果
                            //如果data的长度大于1
                            if (data.length() >1) {


                                //登录成功,跳转
                                //创建一个空意图
                                Intent intent = new Intent();
                                //设置意图的跳转
                                intent.setClass(MainActivity.this, ShowActivity.class);
                                //根据意图启动新的Activity
                            } else {

                                Log.e(TAG, "run: " + "登录失败");
                                // Toast.makeText(MainActivity.this, "登录失败", Toast.LENGTH_SHORT).show();

                                Looper.prepare();
                                Toast.makeText(MainActivity.this, "登录失败", Toast.LENGTH_SHORT).show();
                                Looper.loop();
                            }
                        } catch (IOException e) {

                                                       e.printStackTrace();

                                                  }



                    }
                }).start();
                //TODO 跳转
            }

        });
    }
}
